﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL
{
    public class BaseSqlDBDAL<T> where T : class, new()
    {
        protected T _t;
        public BaseSqlDBDAL()
        {
            _t = new T();
        }
        protected string GetTableName()
        {
            Type type = _t.GetType();
            var tableName = SqlDBHelper.GetTableName(type);
            return tableName;
        }

        public T Get(int id)
        {
            var tableName = GetTableName();
            string sql = SqlDBHelper.GetSqlGet(tableName, id);
            var dbResult = SqlDBHelper.GetDataSet(sql).Tables[0];
            var result = MakeTablePackage(dbResult).FirstOrDefault();
            return result;

        }
        public int Insert(T t)
        {
            var sql = SqlDBHelper.GetSqlInsert<T>(t);
            var result = SqlDBHelper.ExcuteSQL(sql);
            return result;
        }

        public int InsertBatch(List<T> list)
        {
            var dt = list.ToDataTable();
            var result = SqlDBHelper.ExcuteBulkInsert(dt);
            return result;
        }

        public int InsertBatch(DataTable dt)
        {
            var result = SqlDBHelper.ExcuteBulkInsert(dt);
            return result;
        }
        public int Update(T t)
        {
            var sql = SqlDBHelper.GetSqlUpdate<T>(t);
            var result = SqlDBHelper.ExcuteSQL(sql);
            return result;
        }
        public int Delete(int id)
        {
            var tableName = GetTableName();
            var sql = SqlDBHelper.GetSqlDelete(tableName, id);
            var result = SqlDBHelper.ExcuteSQL(sql);
            return result;
        }
        public int DeleteAll()
        {
            var tableName = GetTableName();
            var sql = SqlDBHelper.GetSqlDeleteAll(tableName);
            var result = SqlDBHelper.ExcuteSQL(sql);
            return result;
        }

        public List<T> MakeTablePackage(DataTable _dtGet)
        {
            //try
            //{
            List<T> _lstReturn = new List<T>();

            //获得属性集合
            Type _type = _t.GetType();
            PropertyInfo[] _properties = _type.GetProperties();
            for (int i = 0; i < _dtGet.Rows.Count; i++)
            {
                T _item = new T();
                foreach (DataColumn cloum in _dtGet.Columns)
                {
                    foreach (PropertyInfo _property in _properties)
                    {
                        if (cloum.ColumnName.ToUpper() == _property.Name.ToUpper())
                        {
                            object _value = _dtGet.Rows[i][cloum.ColumnName].ToString();
                            _property.SetValue(_item, _value.ChangeType(_property.PropertyType)
                                , null);
                        }
                    }
                }
                _lstReturn.Add(_item);
            }
            return _lstReturn;
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("打包数据出错-MakeTablePackage");
            //}
        }

        public static IList<T> ConvertToModel(DataTable dt)
        {

            IList<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }



        public static IQueryable<T> ConvertToModel2(DataTable dt)
        {

            IList<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts.AsQueryable();
        }
    }
}
